package com.easycar.common.util;

import com.easycar.common.exception.InitializationException;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.text.DecimalFormat;


public class StringUtils {


    public StringUtils(){
        throw new InitializationException("cannot be instantiated");
    }


    /**
     * 异常转换成String
     * @param t
     * @return
     */
    public static String exception2String(Throwable t) {
        if (t == null)
            return null;
        ByteArrayOutputStream bAOS = new ByteArrayOutputStream();
        try {
            t.printStackTrace(new PrintStream(bAOS));
        } finally {
            try {
                bAOS.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bAOS.toString();
    }


    /**
     * 楼层转换成字符串形式
     * @param floor  楼层
     * @return
     */
    public static String floorToString(int floor) {
        if (floor < 0) {
            return " 负" + Math.abs(floor) + " ";
        }
        return floor + "";
    }


    /**
     * 把路径规划的记录 转换成时间单位
     * @param second
     * @return
     */
    public static String getlyTime(int second) {
        if (second > 3600) {
            int hour = second / 3600;
            int miniate = (second % 3600) / 60;
            return hour + "小时" + miniate + "分钟";
        }
        if (second >= 60) {
            int miniate = second / 60;
            return miniate + "分钟";
        }
        return second + "秒";
    }


    /**
     *
     * @param lenMeter
     * @return
     */
    public static String getlyLength(int lenMeter) {
        if (lenMeter > 10000) // 10 km
        {
            int dis = lenMeter / 1000;
            return dis + "公里";
        }

        if (lenMeter > 1000) {
            float dis = (float) lenMeter / 1000;
            DecimalFormat fnum = new DecimalFormat("##0.0");
            String dstr = fnum.format(dis);
            return dstr + "公里";
        }

        if (lenMeter > 100) {
            int dis = lenMeter / 50 * 50;
            return dis + "米";
        }

        int dis = lenMeter / 10 * 10;
        if (dis == 0) {
            dis = 10;
        }

        return dis + "米";
    }







}
